﻿Imports System.IO
Imports System.Text.RegularExpressions
Imports System.IO.Compression
Imports Map_catcher.Njm_MWH
Imports Shell32
Imports Ionic.Zip

Module map_support
    Public Function adjust_map_files() As Boolean
        adjust_map_files = False
        Try
            For Each map_file In System.IO.Directory.GetFiles(My.Settings.tmp_dir)
                write_map(read_mapfile(map_file), (map_file).Replace(My.Settings.tmp_dir, My.Settings.dest_dir))
            Next
        Catch ex As Exception
            MsgBox("Error in adjust map files")
        End Try
    End Function
    Function get_all_map_files_mwh(ByRef ocr_strings As String)
        Dim new_zip_file As String = My.Settings.tmp_dir & Form1.tb_lot_id.Text & ".zip"
        If Not Form1.debug Then
            Try

                Dim map_proxy As GetMapsProxy
                Dim map_service As New MapService()
                map_proxy = map_service.GetMapsAdvanced("", "", Form1.tb_lot_id.Text, "", My.Settings.ps_string, My.Settings.pss_string, My.Settings.map_type, "", "", "", "")
                Dim mystring As String = System.Text.Encoding.UTF8.GetString(map_proxy.ZipFile)
                Dim oFis As FileStream = New FileStream(new_zip_file, FileMode.Create)
                Dim oBw As BinaryWriter = New BinaryWriter(oFis)
                oBw.Write(map_proxy.ZipFile)
                oBw.Close()
            Catch ex As Exception
                get_all_map_files_mwh = False
                MsgBox("Error in catch maps from: " & My.Settings.map_catcher_Njm_MWH_MapService)
            End Try
            Try
                Dim file As String = new_zip_file
                Dim cu As New ClassUnzip(new_zip_file, My.Settings.source_dir)
                cu.UnzipNow()
                System.IO.File.Delete(new_zip_file)
            Catch ex As Exception
                get_all_map_files_mwh = False
                MsgBox("Error in decompress map package: " & new_zip_file)
            End Try
        End If
        Try
            If ((System.IO.Directory.GetFiles(My.Settings.source_dir).Length) = 0) Then
                MsgBox("Could not find any data for LOT: " & Form1.tb_lot_id.Text)
                Form1.Close()
            End If
            For Each s In System.IO.Directory.GetFiles(My.Settings.source_dir)

                Form1.num_wafers = Form1.num_wafers + 1
                Dim rx As New Regex("([A-Z,0-9]{6}W[0-9]{2}[0-9,A-Z]{2})")           '
                Dim match As MatchCollection = rx.Matches(s.ToString)
                Dim my_new_name As String = match(0).ToString
                Dim my_name As String = my_new_name.Substring(0, my_new_name.Length - 6) & "."
                Dim my_wfr As String = my_new_name.Substring(7, 2)
                Dim new_ocr_string As String = my_name & my_wfr
                System.IO.File.Copy(s, My.Settings.tmp_dir & new_ocr_string)
            Next
            get_all_map_files_mwh = True
        Catch ex As Exception
            get_all_map_files_mwh = False
            MsgBox("Error in renaming map files: " & My.Settings.tmp_dir)
        End Try
    End Function
    Function read_mapfile(mapfile As String) As Array
        Dim maparray(My.Settings.map_rows) As String
        read_mapfile = maparray
        Try
            Using sr As New StreamReader(mapfile, System.Text.Encoding.Default)
                Dim lot_id As String = mapfile.Substring(mapfile.Length - 8, 5)
                Dim wafer_id As String = mapfile.Substring(mapfile.Length - 2, 2)
                Dim row As Integer = 0
                While sr.Peek > -1
                    Dim line As String = sr.ReadLine
                    If line.StartsWith("DEVICE:") Then
                        line = "DEVICE:" & My.Settings.device_name
                    ElseIf line.StartsWith("LOT:") Then
                        Dim ocr_checksum As String = GenerateSEMIOCRCheckSumm(lot_id & "-" & wafer_id)
                        line = "LOT:" & lot_id & "-" & wafer_id & "-" & ocr_checksum
                    Else

                    End If
                    maparray(row) = line
                    read_mapfile = maparray
                    row = row + 1
                End While
            End Using
        Catch ex As Exception
            MsgBox("Error: could not read " & mapfile)
            'Form1.Close()
            read_mapfile = maparray
        End Try
    End Function
    Function write_map(ByVal mapfile() As String, dest_file As String) As Boolean
        Try
            System.IO.File.WriteAllLines(dest_file, mapfile)
            write_map = True
        Catch ex As Exception
            MsgBox("Error: could not write " & dest_file)
            write_map = False
        End Try
    End Function
End Module
